//
//  WmcRecognitionView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/10/18.
//

import UIKit
import YPBedrock

// MARK: - [WmcRecognitionView]

class WmcRecognitionView: UIView {
    // Internal

    var selectedItem: ((WmcWatermarkDetailModel) -> Void)?

    @discardableResult
    static func show() -> WmcRecognitionView {
        let subview = WmcRecognitionView()

        subview.makeUI()
        subview.makeConstraints()
        subview.bindEvent()
        subview.loadData()

        return subview
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: 8.yp.cgSize)
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }

    // Private

    private lazy var viewModel: WmcRecognitionVM = {
        let vm = WmcRecognitionVM()
        return vm
    }()

    private lazy var maskLayer: CAShapeLayer = {
        let maskLayer = CAShapeLayer()
        return maskLayer
    }()

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "请选择想要识别的物体"
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = .wmc_grayH2_323233
        return label
    }()

    private lazy var closeBtn: UIButton = {
        let img = "close".wmc_image.yp.set(tintColor: UIColor.wmc_grayH8_8A8A99)
        let btn = UIButton()
        btn.setImage(img, for: .normal)
        btn.imageEdgeInsets = UIEdgeInsets(top: 1.5, left: 1.5, bottom: 1.5, right: 1.5)
        return btn
    }()

    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH15_E6E6E6
        return view
    }()

    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let width = (UIScreen.main.bounds.width - 33) / 2.0
        let height = width * 0.872
        layout.itemSize = CGSize(width: width, height: height)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 9
        layout.sectionInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
        layout.sectionHeadersPinToVisibleBounds = true

        let view = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.showsVerticalScrollIndicator = true
        view.register(WmcRecognitionListCell.self, forCellWithReuseIdentifier: "WmcRecognitionListCell")
        return view
    }()

    private func makeUI() {
        self.backgroundColor = .white

        addSubview(titleLabel)
        addSubview(closeBtn)
        addSubview(line)
        addSubview(collectionView)
    }

    private func bindEvent() {
        closeBtn.yp.addEvent { _ in
            YPApp.CurrentVC?.dismiss(animated: true)
        }
    }

    private func makeConstraints() {
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(9)
            make.height.equalTo(22)
        }

        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.width.height.equalTo(24)
        }

        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(40)
        }

        collectionView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    private func loadData() {
        viewModel.fetchRecognitionConfigList { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.collectionView.reloadData()
        }
    }
}

// MARK: - UICollectionViewDelegate, UICollectionViewDataSource

extension WmcRecognitionView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WmcRecognitionListCell", for: indexPath) as! WmcRecognitionListCell
        if let model = viewModel.dataSource.yp.object(at: indexPath.row) {
            let isSelected = viewModel.selectedModel?.wmId == model.wmId
            cell.bindModel(model, isSelected: isSelected)
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let model = viewModel.dataSource.yp.object(at: indexPath.row) else { return }
        viewModel.setSelectedModel(model)
        selectedItem?(model)
        collectionView.reloadData()
        YPApp.CurrentVC?.dismiss(animated: true)
    }
}
